<template>
  <el-dialog
    title="编辑学科"
    :visible.sync="dialogFormVisible"
    center
    class="my-dialog"
    width="603px"
    top="10vh"
    @closed="closedHandler"
  >
    <el-form :model="form" :rules="rules" ref="editForm">
      <el-form-item label="学科编号" prop="rid" :label-width="formLabelWidth">
        <el-input v-model="form.rid" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="学科名称" prop="name" :label-width="formLabelWidth">
        <el-input v-model="form.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item
        label="学科简称"
        prop="short_name"
        :label-width="formLabelWidth"
      >
        <el-input v-model="form.short_name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="学科简介" prop="intro" :label-width="formLabelWidth">
        <el-input
          v-model="form.intro"
          type="textarea"
          :rows="2"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="学科备注"
        prop="remark"
        :label-width="formLabelWidth"
      >
        <el-input v-model="form.remark" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="removeForm">取 消</el-button>
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
// 导入发起请求
import { editSubject } from '@/api/subject'
export default {
  name: 'editSubject',
  data () {
    return {
      form: {
        name: '',
        rid: '',
        short_name: '',
        intro: '',
        remark: ''
      },
      rules: {
        rid: [{ required: true, message: '学科编号不能为空', trigger: 'blur' }],
        name: [{ required: true, message: '学科名称不能为空', trigger: 'blur' }]
      },
      dialogFormVisible: false,
      formLabelWidth: '105px'
    }
  },
  methods: {
    closedHandler () {
      this.$refs.editForm.resetFields()
    },
    submitForm () {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          editSubject(this.form).then(res => {
            console.log(res)
            // 关闭弹框
            this.dialogFormVisible = false
            // 传达给父组件中重新渲染页面
            this.$emit('edited')
            // 提示
            this.$message.success('编辑成功')
          })
        } else {
          this.$message.warning('格式有误')
          return false
        }
      })
    },
    removeForm () {
      this.dialogFormVisible = false
    }
  }
}
</script>

<style></style>
